<?php
namespace app\modules\backend\controllers;

use Yii;
use yii\web\Controller;
use app\models\LoginForm;
use yii\filters\VerbFilter;
//use yii\filters\AccessControl;
use app\components\AccessControl;
use app\models\Config;
use app\models\AuthAssignment;

class BaseController extends Controller {

    public $adminId;
    public $adminName;
    public $layout = 'admin'; //设置全局模板为后台模板
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['login', 'error'],
                        'allow' => true,
                    ],
                    [
                        'actions' => ['logout','profile','upload'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    //'logout' => ['post'],
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * @inheritdoc
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'upload' => [
                'class' => 'kucha\ueditor\UEditorAction',
            ],
        ];
    }

    /**
     * 初始化配置信息
     * 网站配置或模板配置等
     */
    public function init() {
        parent::init();
        Yii::$app->params['basic'] = Config::getConfigs('basic');
        //登录用户的信息
        $session = \Yii::$app->session;
        $this->adminId = $session->get('id');
        $this->adminName = $session->get('username');
        
        return true;
    }

    /**
     * 后台登录
     */
    public function actionLogin() {
        $this->layout = false;
        
        if (!Yii::$app->user->isGuest) return $this->goHome();

        $model = new LoginForm();

        if ($model->load(Yii::$app->request->post()) && $model->login()){
            $session = \Yii::$app->session;
            $userId = $session->get('__id');
            $roleName = AuthAssignment::getAssignment($userId);

            $res = $session->set('roleName',$roleName);
            
            return $this->goBack();
        }
        else return $this->render('login', [
                'model' => $model,
            ]);
    }

    /**
     * 退出登录
     */
    public function actionLogout() {
        Yii::$app->user->logout();
        return $this->goHome();
    }
    /**
     * 提示信息
     * @param $type 类型
     * @param $content 内容
     * @param $url 跳转url
     */
    public function message($type, $content, $url = '')
    {
        $referer = Yii::$app->request->getReferrer();
        $click   = $url ? $url : ($referer ? $referer : 'javascript:history.back()');
        $html    = <<<EOT
            <!DOCTYPE html>
            <html lang="zh-hans">
            <head>
                <meta charset="UTF-8">
                <meta name="renderer" content="webkit">
                <meta http-equiv="X-UA-Compatible" content="IE=Edge">
                <title>提示信息页面</title>
                </head>
                <style>
                    .success h1{color: #74CC00;}
                    .error h1{color: red;}
                </style>
            <body>
            <div class="container">
                <div class="wrapper">
                    <div style="padding:30px 15px;text-align:center;" class="cloum mb0 $type">
                        <!--<div class="cloum-title"><h3>提示信息：</h3></div>-->
                        <h1 style="padding: 0 0 10px;font-size: 20px;" class="block">$content</h1>
                        <p>系统自动跳转，如果不想等待，<a style="color:#29a2da;text-decoration:none;" href="$click">点击这里跳转</a></p>
                    </div>
                </div>
            </div>
            <script type="text/javascript">

                var url = "$url";
                setTimeout(function(){
                    if(url){
                        window.location.href = url;
                    }else{
                        history.back();
                    }
                }, 3000);

            </script>
            </body>
            </html>
EOT;
        echo $html;
        exit();
    }

}